interface  DeviceFace {
  index:string,
  name:string,
}
// 获取设备
export const GetDevices = (ffmpegOutput:string) =>{
  const videoRegex = /AVFoundation indev @ [^\]]+\] AVFoundation video devices:([\s\S]+?)AVFoundation indev @ [^\]]+\] AVFoundation audio devices:/;
  const audioRegex = /AVFoundation indev @ [^\]]+\] AVFoundation audio devices:([\s\S]+)/;

  // 提取视频部分
  const videoMatch = ffmpegOutput.match(videoRegex);
  let videoDevices:DeviceFace[] = [];
  if (videoMatch) {
    const videoText = videoMatch[1].trim();
    const videoDeviceRegex = /\[([0-9]+)\]\s([^\[]+)/g;
    let videoDeviceMatch;
    while ((videoDeviceMatch = videoDeviceRegex.exec(videoText)) !== null) {
      videoDevices.push({
        index: videoDeviceMatch[1],
        name: videoDeviceMatch[2].trim()
      });
    }
  }

  // 提取音频部分
  const audioMatch = ffmpegOutput.match(audioRegex);
  let audioDevices:DeviceFace[] = [];
  if (audioMatch) {
    const audioText = audioMatch[1].trim();
    const audioDeviceRegex = /\[([0-9]+)\]\s([^\[]+)/g;
    let audioDeviceMatch;
    while ((audioDeviceMatch = audioDeviceRegex.exec(audioText)) !== null) {
      const deviceName = audioDeviceMatch[2].trim();
      // 去除掉 \n 和 : Input/output error
      const cleanedName = deviceName.replace(/\n.*$/, '');
      audioDevices.push({
        index: audioDeviceMatch[1],
        name: cleanedName
      });
    }
  }

  return {
    videoDevices,
    audioDevices
  }
}
